<?php

namespace App\Http\Controllers\Api\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Admin\BaseController;
use App\Http\Requests\Article\Store;
use App\Http\Requests\Article\Update;
use App\Models\Article;
use File;


class ArticleController extends BaseController
{
    private $return = array(
        'code' => 1,
        'msg' => ''
    );
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Article $article,Request $request)
    {
        $params = $request->all();
        $result = $article->getList($params);
        if(!$result){
            $this->return['msg'] = '查询失败';
        }else{
            $this->return['code'] = 0;
            $this->return['data'] = $result;
        }
        return response()->json($this->return);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Store $request,Article $article)
    {
        $user  = $this->getLoginInfo();
        $data = $request->all();
        $code = randCode(8,5);
        $data['add_uid'] = $user->id;
        $data['code'] = $code;
        $result = $article->add_data($data);
        if(!$result){
            $this->return['msg'] = '新建失败！';
        }else{
            $this->return['code'] = 0;
            $this->return['msg'] = '新建成功！';
            $this->return['data'] = array('code'=>$code);
        }
        return response()->json($this->return);
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($code,Article $article)
    {
        $result = $article->getDataByCode($code);
        if ($result) {
            $this->return['code'] = 0;
            $this->return['data'] = $result->toArray();
        }else {
            $this->return['msg'] = '请求失败';
        }
        return response()->json($this->return);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($code,Article $article)
    {
        $result = $article->getDataByCode($code,1);
        if ($result) {
            $this->return['code'] = 0;
            $this->return['data'] = $result->toArray();
        }else {
            $this->return['msg'] = '请求失败';
        }
        return response()->json($this->return);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Update $request, $code,Article $article)
    {
        $data = $request->all();
        $result = $article->edit_data($code,$data);
        if(!$result){
            $this->return['msg'] = '修改失败！';
        }else{
            $this->return['code'] = 0;
            $this->return['msg'] = '修改成功！';
        }
        return response()->json($this->return);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($code,Article $article)
    {
        $result = $article->delData($code);
        if(!$result){
            $this->return['msg'] = '删除失败！';
        }else{
            $this->return['code'] = 0;
            $this->return['msg'] = '删除成功！';
        }
        return response()->json($this->return);
    }
}
